package tubesprogin4;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ClientFileUtil {
    
    //membuat file kosong, false jika file tersebut sudah ada
    public static boolean makeFile(String path, String filename) throws IOException {
        String fullPath = ClientSettings.CLIENT_REPO_PATH + "/" + path;            
        File f = new File(fullPath, filename);        
        File parent = f.getParentFile();
        if(!parent.exists()) {
            parent.mkdirs();
        }
        if(!f.exists()) {
            return f.createNewFile();
        }        
        return false;
    }
    
    public static boolean makeLogFile(String filename) throws IOException {
        String fullPath = ClientSettings.CLIENT_LOG_PATH;
        File f = new File(fullPath, filename);        
        if(!f.exists()) {
            return f.createNewFile();
        }        
        return false;
    }        
    
    public static boolean makeLogFile(String filename, boolean overwrite) throws IOException {
        String fullPath = ClientSettings.CLIENT_LOG_PATH;
        File f = new File(fullPath, filename);        
        if(!f.exists()) {
            return f.createNewFile();
        }else if(f.exists() && overwrite){
            f.delete();
            return f.createNewFile();            
        }        
        return false;
    }
    
    public static File getLogFile(String filename) throws IOException {
        String fullPath = ClientSettings.CLIENT_LOG_PATH;
        File file = new File(fullPath, filename);
        if(!file.exists()) {            
            file.createNewFile();            
        }
        return file;       
    }
    
    public static void appendFile(String path, String filename, byte[] data) throws IOException {
        String fullPath = ClientSettings.CLIENT_REPO_PATH + "/" + path;
        File file = new File(fullPath, filename);
        
        FileOutputStream fos = new FileOutputStream(file,true);        
        BufferedOutputStream bos = new BufferedOutputStream(fos);        
        bos.write(data);
        bos.flush();
        bos.close();
    }
    
    public static void appendLogFile(String filename, byte[] data) throws FileNotFoundException, IOException {
        String fullPath = ClientSettings.CLIENT_LOG_PATH;
        File file = new File(fullPath, filename);
        
        FileOutputStream fos = new FileOutputStream(file,true);        
        BufferedOutputStream bos = new BufferedOutputStream(fos);        
        bos.write(data);
        bos.flush();
        bos.close();
    }
    
    public static File getFile(String filepath, String filename) throws FileNotFoundException {
        String fullPath = ClientSettings.CLIENT_REPO_PATH + "/" + filepath;
        File f = new File(fullPath, filename);        
        return f;
    }            
    
    public static void main(String[] args) {
        
    }
}
